import 'package:async/async.dart';
import 'package:boss/http/api/api_helper.dart' as apiHelper;
import 'package:boss/models/material.dart' as material;
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class MaterialProvider extends ChangeNotifier {
  List<material.Material> materials = [];

  //搜索关键字
  String searchWord = '';
  List<material.Material> selectedMaterials = [];

  List<material.Material> get searchMaterials {
    if (searchWord == '') {
      return materials;
    }

    return materials.where((item) => Utils.matchChineseWordWithPinyin(item.meterialName, searchWord)).toList();
  }

  setSelectedMaterials({List<material.Material> selectedMaterials}) {
    this.selectedMaterials = selectedMaterials ?? [];
    notifyListeners();
  }

  //初始化数据
  Future<Result<List<material.Material>>> initData() async {
    try {
      var materialResponse = await apiHelper.queryMeterialList();

      if (!materialResponse.successful()) {
        return Result.error(materialResponse.msg);
      }

      materials = materialResponse.data;
      notifyListeners();

      return Result.value(materialResponse.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  bool checkRequestData(material.Material data) {
    if (data.meterialName == null || data.meterialName == '') {
      toast('加料名称不能为空');
      return false;
    }
    if (data.meterialPrice == null || data.meterialPrice == -1) {
      toast('请输入正确的价格格式');
      return false;
    }

    return true;
  }

  //新增加料
  addMaterial(material.Material data) async {
    try {
      if (!checkRequestData(data)) return;

      showLoadingToast();

      var res = await apiHelper.addMeterial(
        meterialName: data.meterialName,
        meterialPrice: data.meterialPrice,
      );
      if (!res.successful()) {
        toast(res.msg);
        return;
      }

      materials.add(res.data);
      notifyListeners();

      Navigator.pop(GlobalPageContext.instance.context, res.data);
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  //编辑修改加料
  updateMaterial(material.Material data) async {
    try {
      if (!checkRequestData(data)) return;

      showLoadingToast();

      var res = await apiHelper.updateMeterial(data);
      if (!res.successful()) {
        toast(res.msg);
        return;
      }

      materials.forEach((item) {
        if (item.id == data.id) {
          item.meterialName = data.meterialName;
          item.meterialPrice = data.meterialPrice;
        }
      });
      notifyListeners();

      Navigator.pop(GlobalPageContext.instance.context);
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  //删除加料
  deleteMaterialById(int id) async {
    try {
      showLoadingToast();
      var result = await apiHelper.deleteMeterial(id);
      if (!result.successful()) {
        toast(result.msg);
        return;
      }
      materials.removeWhere((item) => item.id == id);
      notifyListeners();
      toast('删除成功');

      Navigator.pop(GlobalPageContext.instance.context);
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  //修改搜索关键字
  setSearchWord(String word) {
    searchWord = word;
    notifyListeners();
  }

  //清楚搜索关键字
  clearSearchWord() {
    searchWord = '';
    notifyListeners();
  }

  //切换加料选中状态
  toggleSelected(material.Material data) {
    var isSelected = selectedMaterials.any((item) => item.id == data.id);
    isSelected ? selectedMaterials.removeWhere((item) => item.id == data.id) : selectedMaterials.add(data);
    notifyListeners();
  }
}
